package cn.com.callback

import groovy.json.*;
import java.util.Map;

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

import java.text.ParseException
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import cn.com.elite.EliteBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.json.JSONObject;

import com.alibaba.fastjson.JSON

import org.json.JSONArray;
import org.json.JSONException;

import cn.com.elite.Util;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;
/**
 *
 * @Title: HST_job_LoginOut
 * @Description:华盛证券 SSO统一登出接口 --内部接口
 * @author: chieh
 * @date 2021-07-07 12:30:00
 *
 */
public class HST_job_LoginOut implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(HST_job_LoginOut.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String paramString){

		//日志标识，方便查找
		log.debug("----------------------------------start HST_job_LoginOut  开始执行任务 -----------------------------");
		//判断是否有需要推送的任务
		def getLoginoutSelectSQL = eliteBean.chaxun_sql("select b.KEYID as keyid,s.loginname,s.Stringfield1 as token,b.CLIENTIP as remoteAddr from  staff s  left join (select KEYID,STAFFID,CLIENTIP,LOGINDATE from t_logintablehis where LOGINDATE>str_to_date(DATE_FORMAT(NOW(),'%Y-%m-%d'),'%Y-%m-%d %H:%i:%s') and LOGOUTDATE is not null and CLIENTFLAG=1 and LOGOUTTYPE=1 and loginout=0 order by LOGINDATE desc) b on s.STAFF_ID=b.STAFFID where  s.STAFF_ID not in (select STAFF_ID from webloginlist)  and s.Stringfield1 is not null and b.CLIENTIP is not null","dataSource");
		def Loginoutnum=getLoginoutSelectSQL.size();
		log.debug("Loginoutnum:"+Loginoutnum);

		if(Loginoutnum> 0) {
			log.debug("--------------------------开始执行登出任务--------------------------");
			String keyid ="";
			String loginname ="";
			String token =""; 
			String remoteAddr ="";

			def status=0;
			String json1,injson,returnDealMessage="";
			try{
				
				for(int i=0;i<Loginoutnum;i++) {
					Map<String, Object> param = new HashMap<String, Object>();
					
					keyid = getLoginoutSelectSQL.get(i).get("keyid");
					log.debug("keyid:	"+keyid);
					loginname = getLoginoutSelectSQL.get(i).get("loginname");
					token= getLoginoutSelectSQL.get(i).get("token");
					remoteAddr= getLoginoutSelectSQL.get(i).get("remoteAddr");
					param.put("loginname",loginname);
					param.put("token",token);
					param.put("remoteAddr", remoteAddr);
					String  paramS= JSON.toJSONString(param);
					log.debug("入参:	"+paramS);
					
					Map<String, String> headMap = new HashMap<String, String>();
					headMap.put("Content-Type", "application/json");
					String sysurl = getProperty.get("crmurl");//配置本地接口地址
					//华盛 调度内部短信发送接口执行
					sysurl = sysurl+"aipHSTLoginOut";
					log.debug("sysurl:	"+sysurl);
					injson=Util.doHttpPostUseHead(sysurl,paramS,"UTF-8",100000,0,headMap);
					
					log.debug("调用返回信息:	"+injson);
					JSONObject jsonObject = new JSONObject(injson);
					String code = GetJV(jsonObject, "result");
					if(code=='1'){
						eliteBean.update("update t_logintablehis set loginout=1 where KEYID ='"+keyid+"'", "dataSource");
					}else{
						eliteBean.update("update t_logintablehis set loginout=2 where KEYID ='"+keyid+"'", "dataSource");
					}
				}

			} catch (Exception e) {
				returnDealMessage="接口处理异常" + e.getMessage();
				log.error(returnDealMessage);
			}
		}
		log.debug("HST_job_LoginOut当前无账号登出送任务--------------------------");

	}
	
	// 解析Json值
    public static String GetJV(JSONObject inJson, String getKey) {
        String Rev_Value = null;
        try {
            Rev_Value = inJson.getString(getKey);
        } catch (Exception e) {
            try {
                Rev_Value = String.valueOf(inJson.getInt(getKey));
            } catch (Exception e1) {
                try {
                    Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
                } catch (Exception e2) {
                    try {
                        Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
                    } catch (Exception e3) {
                        Rev_Value = "";
                    }
                }
            }
        } finally {
            if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
                Rev_Value = "";
            } else {
            }
        }
        log.debug("解析-k=" + getKey + " v=" + Rev_Value);
        return Rev_Value;
    }

}